/**
 * 
 */
package com.qf.apps;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.qf.apps.vo.LoginForm;
import com.qf.domain.User;
import com.qf.service.UserService;

/**
 * @author 123
 * 
 */
public class LoginController extends SimpleFormController {
	private UserService userService;

	protected Map referenceData(HttpServletRequest request) throws Exception {
		return new HashMap();
	}

	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		System.out.println("formBackingObject");
		return new LoginForm();
	}

	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		LoginForm loginForm = (LoginForm) command;
		System.out.println(loginForm.getUserName());
		User user = userService.login(loginForm.getUserName(), loginForm
				.getPassword());

		if (user == null) {
			errors.rejectValue("password", "invalid");
			return showForm(request, response, errors);
		} else {
			return new ModelAndView("index");
		}
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}
